<?php
/**
 * 管理员后台会员操作类
 */

defined('IN_PHPCMS') or exit('No permission resources.');
//模型缓存路径
define('CACHE_MODEL_PATH', CACHE_PATH . 'caches_model' . DS . 'caches_data' . DS);

pc_base::load_app_class('admin', 'admin', 0);
pc_base::load_sys_class('format', '', 0);
pc_base::load_sys_class('form', '', 0);
pc_base::load_app_func('util', 'content');

class member extends admin {

    private $db, $verify_db;

    function __construct() {
        parent::__construct();
        $this->db = pc_base::load_model('member_model');
        $this->_init_phpsso();
    }

    /**
     * defalut
     */
    function init() {
        $show_header = $show_scroll = true;
        pc_base::load_sys_class('form', '', 0);
        $this->verify_db = pc_base::load_model('member_verify_model');

        //搜索框
        $keyword = isset($_GET['keyword']) ? $_GET['keyword'] : '';
        $type = isset($_GET['type']) ? $_GET['type'] : '';
        $groupid = isset($_GET['groupid']) ? $_GET['groupid'] : '';
        $start_time = isset($_GET['start_time']) ? $_GET['start_time'] : date('Y-m-d', SYS_TIME - date('t', SYS_TIME) * 86400);
        $end_time = isset($_GET['end_time']) ? $_GET['end_time'] : date('Y-m-d', SYS_TIME);
        $grouplist = getcache('grouplist');
        foreach ($grouplist as $k => $v) {
            $grouplist[$k] = $v['name'];
        }

        $memberinfo['totalnum'] = $this->db->count();
        $memberinfo['vipnum'] = $this->db->count(array('vip' => 1));
        $memberinfo['verifynum'] = $this->verify_db->count(array('status' => 0));

        $todaytime = strtotime(date('Y-m-d', SYS_TIME));
        $memberinfo['today_member'] = $this->db->count("`regdate` > '$todaytime'");
       echo $this->admin_tpl('member_init');exit();
        include $this->admin_tpl('member_init');
    }

    /**
     * 会员搜索
     */
    function search() {

        //搜索框
        $keyword = isset($_GET['keyword']) ? $_GET['keyword'] : '';
        $type = isset($_GET['type']) ? $_GET['type'] : '';
        $groupid = isset($_GET['groupid']) ? $_GET['groupid'] : '';
        $modelid = isset($_GET['modelid']) ? $_GET['modelid'] : '';

        //站点信息
        $sitelistarr = getcache('sitelist', 'commons');
        $siteid = isset($_GET['siteid']) ? intval($_GET['siteid']) : '0';
        foreach ($sitelistarr as $k => $v) {
            $sitelist[$k] = $v['name'];
        }

        $status = isset($_GET['status']) ? $_GET['status'] : '';
        $amount_from = isset($_GET['amount_from']) ? $_GET['amount_from'] : '';
        $amount_to = isset($_GET['amount_to']) ? $_GET['amount_to'] : '';
        $point_from = isset($_GET['point_from']) ? $_GET['point_from'] : '';
        $point_to = isset($_GET['point_to']) ? $_GET['point_to'] : '';

        $start_time = isset($_GET['start_time']) ? $_GET['start_time'] : '';
        $end_time = isset($_GET['end_time']) ? $_GET['end_time'] : date('Y-m-d', SYS_TIME);
        $grouplist = getcache('grouplist');
        foreach ($grouplist as $k => $v) {
            $grouplist[$k] = $v['name'];
        }
        //会员所属模型
        $modellistarr = getcache('member_model', 'commons');
        foreach ($modellistarr as $k => $v) {
            $modellist[$k] = $v['name'];
        }

        if (isset($_GET['search'])) {

            //默认选取一个月内的用户，防止用户量过大给数据造成灾难
            $where_start_time = strtotime($start_time) ? strtotime($start_time) : 0;
            $where_end_time = strtotime($end_time) + 86400;
            //开始时间大于结束时间，置换变量
            if ($where_start_time > $where_end_time) {
                $tmp = $where_start_time;
                $where_start_time = $where_end_time;
                $where_end_time = $tmp;
                $tmptime = $start_time;

                $start_time = $end_time;
                $end_time = $tmptime;
                unset($tmp, $tmptime);
            }


            $where = '';

            //如果是超级管理员角色，显示所有用户，否则显示当前站点用户
            if ($_SESSION['roleid'] == 1) {
                if (!empty($siteid)) {
                    $where .= "`siteid` = '$siteid' AND ";
                }
            } else {
                $siteid = get_siteid();
                $where .= "`siteid` = '$siteid' AND ";
            }

            if ($status) {
                $islock = $status == 1 ? 1 : 0;
                $where .= "`islock` = '$islock' AND ";
            }

            if ($groupid) {
                $where .= "`groupid` = '$groupid' AND ";
            }

            if ($modelid) {
                $where .= "`modelid` = '$modelid' AND ";
            }
            $where .= "`regdate` BETWEEN '$where_start_time' AND '$where_end_time' AND ";

            //资金范围
            if ($amount_from) {
                if ($amount_to) {
                    if ($amount_from > $amount_to) {
                        $tmp = $amount_from;
                        $amount_from = $amount_to;
                        $amount_to = $tmp;
                        unset($tmp);
                    }
                    $where .= "`amount` BETWEEN '$amount_from' AND '$amount_to' AND ";
                } else {
                    $where .= "`amount` > '$amount_from' AND ";
                }
            }
            //点数范围
            if ($point_from) {
                if ($point_to) {
                    if ($point_from > $point_to) {
                        $tmp = $amount_from;
                        $point_from = $point_to;
                        $point_to = $tmp;
                        unset($tmp);
                    }
                    $where .= "`point` BETWEEN '$point_from' AND '$point_to' AND ";
                } else {
                    $where .= "`point` > '$point_from' AND ";
                }
            }

            if ($keyword) {
                if ($type == '1') {
                    $where .= "`username` LIKE '%$keyword%'";
                } elseif ($type == '2') {
                    $where .= "`userid` = '$keyword'";
                } elseif ($type == '3') {
                    $where .= "`email` like '%$keyword%'";
                } elseif ($type == '4') {
                    $where .= "`regip` = '$keyword'";
                } elseif ($type == '5') {
                    $where .= "`nickname` LIKE '%$keyword%'";
                } else {
                    $where .= "`username` like '%$keyword%'";
                }
            } else {
                $where .= '1';
            }

        } else {
            $where = '';
        }

        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;
        $memberlist = $this->db->listinfo($where, 'userid DESC', $page, 15);
        $pages = $this->db->pages;
        $big_menu = array('?m=member&c=member&a=manage&menuid=72', L('member_research'));
        include $this->admin_tpl('member_list');
    }

    public function export() {
        header('Content-Type: application/vnd.ms-excel');
        header('Content-Disposition: attachment;filename="gaojian.member.csv"');
        header('Cache-Control: max-age=0');

        $fp = fopen('php://output', 'a');

        // 输出Excel列名信息
        $in = $this->db->get_fields();
        $in = array_keys($in);
        foreach ($in as &$v) {
            $v = iconv('utf-8', 'gb2312', $v);
        }
        fputcsv($fp, $in);

        $arr = $this->db->select();
        foreach ($arr as $row) {
            $in = [];
            foreach ($row as $v) {
                $in[] = iconv('utf-8', 'gb2312', $v);
            }
            fputcsv($fp, $in);
        }
    }

    /**
     * member list
     */
    public function manage() {
        $sitelistarr = getcache('sitelist', 'commons');
        foreach ($sitelistarr as $k => $v) {
            $sitelist[$k] = $v['name'];
        }

        $groupid = isset($_GET['groupid']) ? intval($_GET['groupid']) : '';
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        $where = '';

        $memberlist_arr = $this->db->listinfo($where, 'userid DESC', $page, 15);
        $pages = $this->db->pages;

        //搜索框
        $keyword = isset($_GET['keyword']) ? $_GET['keyword'] : '';
        $type = isset($_GET['type']) ? $_GET['type'] : '';
        $start_time = isset($_GET['start_time']) ? $_GET['start_time'] : '';
        $end_time = isset($_GET['end_time']) ? $_GET['end_time'] : date('Y-m-d', SYS_TIME);
        $grouplist = getcache('grouplist');
        foreach ($grouplist as $k => $v) {
            $grouplist[$k] = $v['name'];
        }

        //会员所属模型
        $modellistarr = getcache('member_model', 'commons');
        foreach ($modellistarr as $k => $v) {
            $modellist[$k] = $v['name'];
        }

        //查询会员头像
        foreach ($memberlist_arr as $k => $v) {
            $memberlist[$k] = $v;
            $memberlist[$k]['avatar'] = get_memberavatar($v['phpssouid']);
        }

        $big_menu = array('javascript:window.top.art.dialog({id:\'add\',iframe:\'?m=member&c=member&a=add\', title:\'' . L('member_add') . '\', width:\'700\', height:\'500\', lock:true}, function(){var d = window.top.art.dialog({id:\'add\'}).data.iframe;var form = d.document.getElementById(\'dosubmit\');form.click();return false;}, function(){window.top.art.dialog({id:\'add\'}).close()});void(0);', L('member_add'));
        include $this->admin_tpl('member_list');
    }

    /**
     * add member
     */
    function add() {
        header("Cache-control: private");
        if (isset($_POST['dosubmit'])) {
            $info = array();
            if (!$this->_checkname($_POST['info']['username'])) {
                showmessage(L('member_exist'));
            }
            $info = $this->_checkuserinfo($_POST['info']);
            if (!$this->_checkpasswd($info['password'])) {
                showmessage(L('password_format_incorrect'));
            }
            $info['regip'] = ip();
            $info['overduedate'] = strtotime($info['overduedate']);

            $status = $this->client->ps_member_register($info['username'], $info['password'], $info['email'], $info['regip']);

            if ($status > 0) {
                unset($info[pwdconfirm]);
                $info['phpssouid'] = $status;
                //取phpsso密码随机数
                $memberinfo = $this->client->ps_get_member_info($status);
                $memberinfo = unserialize($memberinfo);
                $info['encrypt'] = $memberinfo['random'];
                $info['password'] = password($info['password'], $info['encrypt']);
                $info['regdate'] = $info['lastdate'] = SYS_TIME;

                $this->db->insert($info);
                if ($this->db->insert_id()) {
                    showmessage(L('operation_success'), '?m=member&c=member&a=add', '', 'add');
                }
            } elseif ($status == -4) {
                showmessage(L('username_deny'), HTTP_REFERER);
            } elseif ($status == -5) {
                showmessage(L('email_deny'), HTTP_REFERER);
            } else {
                showmessage(L('operation_failure'), HTTP_REFERER);
            }
        } else {
            $show_header = $show_scroll = true;
            $siteid = get_siteid();
            //会员组缓存
            $group_cache = getcache('grouplist', 'member');
            foreach ($group_cache as $_key => $_value) {
                $grouplist[$_key] = $_value['name'];
            }
            //会员模型缓存
            $member_model_cache = getcache('member_model', 'commons');
            foreach ($member_model_cache as $_key => $_value) {
                if ($siteid == $_value['siteid']) {
                    $modellist[$_key] = $_value['name'];
                }
            }

            include $this->admin_tpl('member_add');
        }

    }

    /**
     * edit member
     */
    function edit() {
        if (isset($_POST['dosubmit'])) {
            //会员基本信息
            $info = $this->_checkuserinfo($_POST['info'], 1);
            $userid = $info['userid'];

            $userinfo = $this->db->get_one(['userid' => $userid]);
            if (empty($userinfo)) {
                showmessage(L('user_not_exist') . L('or') . L('no_permission'), HTTP_REFERER);
            }

            unset($info['pwdconfirm']);
            unset($info['userid']);
            unset($info['username']);

            //如果密码不为空，修改用户密码。
            if (isset($info['password']) && !empty($info['password'])) {
                $info['password'] = md5($info['password']);
            } else {
                unset($info['password']);
            }

            $this->db->update($info, ['userid' => $userid]);

            showmessage(L('operation_success'), '?m=member&c=member&a=manage', '', 'edit');
        } else {
            $show_header = $show_scroll = true;
            $siteid = get_siteid();
            $userid = isset($_GET['userid']) ? $_GET['userid'] : showmessage(L('illegal_parameters'), HTTP_REFERER);

            //会员组缓存
            $group_cache = getcache('grouplist', 'member');
            foreach ($group_cache as $_key => $_value) {
                $grouplist[$_key] = $_value['name'];
            }

            //会员模型缓存
            $member_model_cache = getcache('member_model', 'commons');
            foreach ($member_model_cache as $_key => $_value) {
                if ($siteid == $_value['siteid']) {
                    $modellist[$_key] = $_value['name'];
                }
            }

            $memberinfo = $this->db->get_one(['userid' => $userid]);

            if (empty($memberinfo)) {
                showmessage(L('user_not_exist') . L('or') . L('no_permission'), HTTP_REFERER);
            }

            $show_dialog = 1;
            include $this->admin_tpl('member_edit');
        }
    }

    /**
     * delete member
     */
    function delete() {
        $uidarr = isset($_POST['userid']) ? $_POST['userid'] : showmessage(L('illegal_parameters'), HTTP_REFERER);
        $uidarr = array_map('intval', $uidarr);
        $where = to_sqls($uidarr, '', 'userid');
        $phpsso_userinfo = $this->db->listinfo($where);
        $phpssouidarr = array();
        if (is_array($phpsso_userinfo)) {
            foreach ($phpsso_userinfo as $v) {
                if (!empty($v['phpssouid'])) {
                    $phpssouidarr[] = $v['phpssouid'];
                }
            }
        }
        //查询用户信息
        $userinfo_arr = $this->db->select($where, "userid, modelid");
        $userinfo = array();
        if (is_array($userinfo_arr)) {
            foreach ($userinfo_arr as $v) {
                $userinfo[$v['userid']] = $v['modelid'];
            }
        }
        //delete phpsso member first
        if (!empty($phpssouidarr)) {
            $status = $this->client->ps_delete_member($phpssouidarr, 1);
            if ($status > 0) {
                if ($this->db->delete($where)) {

                    //删除用户模型用户资料
                    foreach ($uidarr as $v) {
                        if (!empty($userinfo[$v])) {
                            $this->db->set_model($userinfo[$v]);
                            $this->db->delete(array('userid' => $v));
                        }
                    }

                    showmessage(L('operation_success'), HTTP_REFERER);
                } else {
                    showmessage(L('operation_failure'), HTTP_REFERER);
                }
            } else {
                showmessage(L('operation_failure'), HTTP_REFERER);
            }
        } else {
            if ($this->db->delete($where)) {
                showmessage(L('operation_success'), HTTP_REFERER);
            } else {
                showmessage(L('operation_failure'), HTTP_REFERER);
            }
        }
    }

    /**
     * lock member
     */
    function lock() {
        if (isset($_POST['userid'])) {
            $uidarr = isset($_POST['userid']) ? $_POST['userid'] : showmessage(L('illegal_parameters'), HTTP_REFERER);
            $where = to_sqls($uidarr, '', 'userid');
            $this->db->update(array('islock' => 1), $where);
            showmessage(L('member_lock') . L('operation_success'), HTTP_REFERER);
        } else {
            showmessage(L('operation_failure'), HTTP_REFERER);
        }
    }

    /**
     * unlock member
     */
    function unlock() {
        if (isset($_POST['userid'])) {
            $uidarr = isset($_POST['userid']) ? $_POST['userid'] : showmessage(L('illegal_parameters'), HTTP_REFERER);
            $where = to_sqls($uidarr, '', 'userid');
            $this->db->update(array('islock' => 0), $where);
            showmessage(L('member_unlock') . L('operation_success'), HTTP_REFERER);
        } else {
            showmessage(L('operation_failure'), HTTP_REFERER);
        }
    }

    /**
     * move member
     */
    function move() {
        if (isset($_POST['dosubmit'])) {
            $uidarr = isset($_POST['userid']) ? $_POST['userid'] : showmessage(L('illegal_parameters'), HTTP_REFERER);
            $groupid = isset($_POST['groupid']) && !empty($_POST['groupid']) ? $_POST['groupid'] : showmessage(L('illegal_parameters'), HTTP_REFERER);

            $where = to_sqls($uidarr, '', 'userid');
            $this->db->update(array('groupid' => $groupid), $where);
            showmessage(L('member_move') . L('operation_success'), HTTP_REFERER, '', 'move');
        } else {
            $show_header = $show_scroll = true;
            $grouplist = getcache('grouplist');
            foreach ($grouplist as $k => $v) {
                $grouplist[$k] = $v['name'];
            }

            $ids = isset($_GET['ids']) ? explode(',', $_GET['ids']) : showmessage(L('illegal_parameters'), HTTP_REFERER);
            array_pop($ids);
            if (!empty($ids)) {
                $where = to_sqls($ids, '', 'userid');
                $userarr = $this->db->listinfo($where);
            } else {
                showmessage(L('illegal_parameters'), HTTP_REFERER, '', 'move');
            }

            include $this->admin_tpl('member_move');
        }
    }

    //会员详情
    public function memberinfo() {
        $show_header = false;

        $userid = !empty($_GET['userid']) ? intval($_GET['userid']) : '';
        $username = !empty($_GET['username']) ? trim($_GET['username']) : '';
        if (!empty($userid)) {
            $memberinfo = $this->db->get_one(array('userid' => $userid));
        } elseif (!empty($username)) {
            $memberinfo = $this->db->get_one(array('username' => $username));
        } else {
            showmessage(L('illegal_parameters'), HTTP_REFERER);
        }

        if (empty($memberinfo)) {
            showmessage(L('user') . L('not_exists'), HTTP_REFERER);
        }

        include $this->admin_tpl('member_moreinfo');
    }

    /*
     * 通过linkageid获取名字路径
     */
    private function _get_linkage_fullname($linkageid, $linkagelist) {
        $fullname = '';
        if ($linkagelist['data'][$linkageid]['parentid'] != 0) {
            $fullname = $this->_get_linkage_fullname($linkagelist['data'][$linkageid]['parentid'], $linkagelist);
        }
        //所在地区名称
        $return = $fullname . $linkagelist['data'][$linkageid]['name'] . '>';
        return $return;
    }

    private function _checkuserinfo($data, $is_edit = 0) {
        if (!is_array($data)) {
            showmessage(L('need_more_param'));
            return false;
        } elseif (!is_username($data['username']) && !$is_edit) {
            showmessage(L('username_format_incorrect'));
            return false;
        } elseif (!isset($data['userid']) && $is_edit) {
            showmessage(L('username_format_incorrect'));
            return false;
        } elseif (empty($data['email']) || !is_email($data['email'])) {
            showmessage(L('email_format_incorrect'));
            return false;
        }
        return $data;
    }

    private function _checkpasswd($password) {
        if (!is_password($password)) {
            return false;
        }
        return true;
    }

    private function _checkname($username) {
        $username = trim($username);
        if ($this->db->get_one(array('username' => $username))) {
            return false;
        }
        return true;
    }

    /**
     * 初始化phpsso
     * about phpsso, include client and client configure
     * @return string phpsso_api_url phpsso地址
     */
    private function _init_phpsso() {
        pc_base::load_app_class('client', '', 0);
        define('APPID', pc_base::load_config('system', 'phpsso_appid'));
        $phpsso_api_url = pc_base::load_config('system', 'phpsso_api_url');
        $phpsso_auth_key = pc_base::load_config('system', 'phpsso_auth_key');
        $this->client = new client($phpsso_api_url, $phpsso_auth_key);
        return $phpsso_api_url;
    }

    /**
     * 检查用户名
     * @param string $username 用户名
     * @return $status {-4：用户名禁止注册;-1:用户名已经存在 ;1:成功}
     */
    public function public_checkname_ajax() {
        $username = isset($_GET['username']) && trim($_GET['username']) ? trim($_GET['username']) : exit(0);
        if (CHARSET != 'utf-8') {
            $username = iconv('utf-8', CHARSET, $username);
            $username = addslashes($username);
        }

        $status = $this->client->ps_checkname($username);

        if ($status == -4 || $status == -1) {
            exit('0');
        } else {
            exit('1');
        }

    }

    /**
     * 检查邮箱
     * @param string $email
     * @return $status {-1:email已经存在 ;-5:邮箱禁止注册;1:成功}
     */
    public function public_checkemail_ajax() {
        $email = isset($_GET['email']) && trim($_GET['email']) ? trim($_GET['email']) : exit(0);

        $status = $this->client->ps_checkemail($email);
        if ($status == -5) {    //禁止注册
            exit('0');
        } elseif ($status == -1) {    //用户名已存在，但是修改用户的时候需要判断邮箱是否是当前用户的
            if (isset($_GET['phpssouid'])) {    //修改用户传入phpssouid
                $status = $this->client->ps_get_member_info($email, 3);
                if ($status) {
                    $status = unserialize($status);    //接口返回序列化，进行判断
                    if (isset($status['uid']) && $status['uid'] == intval($_GET['phpssouid'])) {
                        exit('1');
                    } else {
                        exit('0');
                    }
                } else {
                    exit('0');
                }
            } else {
                exit('0');
            }
        } else {
            exit('1');
        }
    }

}

?>